import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import RegisterView from "@/views/RegisterView.vue";
import BookView from "@/views/BookView.vue";
import PersionalView from "@/views/PersionalView.vue";
import BookUserView from "@/views/BookUserView.vue";
import AdminUserView from "@/views/AdminUserView.vue";
import ScoreView from "@/views/ScoreView.vue";
import LiveView from "@/views/LiveView.vue";

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path: '/register',
    name:'register',
    component: RegisterView
  },
  {
    path: '/persional',
    name:'persional',
    component: PersionalView
  },
  {
    path: '/',
    name: 'layout',
    component: () => import('../views/LayoutView.vue'),
    children: [
      {
        path: '',
        name: 'home',
        component: HomeView
      },
      {
        path: 'admin',
        name: 'admin',
        component: () => import('../views/AdminView.vue')
      },
      {
        path: 'book',
        name: 'book',
        component: BookView
      },
      {
        path: 'adminuser',
        name: 'adminuser',
        component: AdminUserView
      },
      {
        path: 'bookuser',
        name: 'bookuser',
        component: BookUserView
      },
      {
        path: 'score',
        name: 'score',
        component: ScoreView
      },
      {
        path: 'live',
        name: 'live',
        component: LiveView
      },
      {
        path: 'schoolsupplies',
        name: 'schoolsupplies',
        component: () => import('../views/SchoolSuppliesView.vue')
      },
      {
        path: 'electronicdevice',
        name: 'electronicdevice',
        component: () => import('../views/ElectronicDeviceView.vue')
      },
      {
        path: 'campustransportation',
        name: 'campustransportation',
        component: () => import('../views/CampusTransportationView.vue')
      },
      {
        path: 'sportsupplies',
        name: 'sportsupplies',
        component: () => import('../views/SportSuppliesView.vue')
      },
      {
        path: 'othergoods',
        name: 'othergoods',
        component: () => import('../views/OtherGoodsView.vue')
      },

      {
        path: 'personallog',
        name: 'personallog',
        component: () => import('../views/Personallog.vue')
      },
      {
        path: 'notice',
        name: 'notice',
        component: () => import('../views/NoticeView.vue')
      },
      {
        path: 'reserve',
        name: 'reserve',
        component: () => import('../views/ReserveView.vue')
      },
      {
        path: 'reservebook',
        name: 'reservebook',
        component: () => import('../views/ReserveBookView.vue')
      },
      {
        path: 'reservelive',
        name: 'reservelive',
        component: () => import('../views/ReserveLiveView.vue')
      },
      {
        path: 'reservestudy',
        name: 'reservestudy',
        component: () => import('../views/ReserveStudyView.vue')
      },
      {
        path: 'data',
        name: 'data',
        component: () => import('../views/DataView.vue')
      },
      {
        path: 'databook',
        name: 'databook',
        component: () => import('../views/DatabookView.vue')
      },
      {
        path: 'datalive',
        name: 'datalive',
        component: () => import('../views/DataliveView.vue')
      },
      {
        path: 'datastudy',
        name: 'datastudy',
        component: () => import('../views/DatastudyView.vue')
      },
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 前端路由守卫
router.beforeEach((to, from, next) => {
  const user = localStorage.getItem("user");
  if (!user && to.path !== '/login' && to.path!== '/register') {
    return next("/login");
  }
  next();
})


export default router
